FROM centos:7

# 维护者
MAINTAINER Tony <cheerfultony@gmail.com>

# 设置 PHP 版本 及 PATH（固化环境变量）
ENV PHP_VERSION 7.1.5
ENV PATH /usr/local/php/bin:/usr/local/php/sbin:$PATH

# 切换工作目录
WORKDIR /tmp

# 设置yum源
RUN mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup
COPY CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo
RUN yum clean all \
  && yum makecache

# 调整时区(有bug)
# timedatectl set-timezone Asia/Shanghai
RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime

# 关闭SELinux

# 关闭防火墙

# 解压swoole和redis包
ADD ./swoole-swoole-v4.2.2.tar.gz /tmp
ADD ./phpredis-php7.tar.gz /tmp


RUN set -x \
  && yum update -y \
  && yum install epel-release -y \
  # centos7 下 yum安装libmcrypt会有找不到的情况，需要手动安装
  && yum install -y wget git gcc gcc-c++ make automake autoconf perl file tar re2c libjpeg libpng libjpeg-devel libpng-devel libjpeg-turbo freetype freetype-devel \
        libcurl-devel libxml2-devel libjpeg-turbo-devel libXpm-devel libXpm libicu-devel libmcrypt-devel libxslt-devel libxslt openssl openssl-devel bzip2-devel pcre-devel \

# 编译 PHP
  && wget http://cn2.php.net/distributions/php-$PHP_VERSION.tar.gz \
  && tar -zxf php-$PHP_VERSION.tar.gz \
  && cd php-$PHP_VERSION \
  && ./configure \
       --prefix=/usr/local/php \
       --with-config-file-path=/usr/local/php/etc/ \
       --with-config-file-scan-dir=/usr/local/php/conf.d/ \
       --enable-fpm \
       --enable-cgi \
       --with-fpm-user=nginx  \
       --with-fpm-group=nginx \
       --disable-phpdbg \
       --enable-mbstring \
       --enable-calendar \
       --with-xsl \
       --with-openssl \
       --enable-soap \
       --enable-zip \
       --enable-shmop \
       --enable-sockets \
       --with-gd \
       --with-freetype-dir=/usr/include/freetype2/freetype \
       --with-jpeg-dir \
       --with-png-dir \
       --with-xpm-dir \
       --with-xmlrpc \
       --enable-pcntl \
       --enable-intl \
       --with-mcrypt \
       --enable-sysvsem \
       --enable-sysvshm \
       --enable-sysvmsg \
       --enable-opcache \
       --with-iconv \
       --with-bz2 \
       --with-curl \
       --enable-mysqlnd \
       --with-mysqli=mysqlnd \
       --with-pdo-mysql=mysqlnd \
       --with-zlib \
       --with-gettext \
  && make \
  && make install \

  # 复制配置文件
  && cp /tmp/php-$PHP_VERSION/php.ini-production /usr/local/php/etc/php.ini \
  && cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf \
  && cp /usr/local/php/etc/php-fpm.d/www.conf.default /usr/local/php/etc/php-fpm.d/www.conf \
  && echo 'extension=swoole.so' >> /usr/local/php/etc/php.ini \
  && echo 'extension=redis.so' >> /usr/local/php/etc/php.ini \

  # 加入环境变量
  # && echo "export PATH=/usr/local/php/bin:/usr/local/php/sbin:$PATH" >> /etc/profile \
  # && source /etc/profile \

  # 安装swoole
  && cd /tmp/swoole \
  && phpize \
  && ./configure \
  && make \
  && make install \

  # 安装redis
  && cd /tmp/phpredis-php7 \
  && phpize \
  && ./configure \
  && make \
  && make install \

  # 安装 composer 由于composer下载慢或失败的问题，这里不进行安装

  # 删除安装文件
  && rm -rf /tmp/* \
  && yum clean all \
  && rm -rf /var/cache/yum \

  # 创建对外目录
  && mkdir /root/log \
  && mkdir /root/apps

WORKDIR /root
CMD [ "php", "./apps/server.php" ]